package com.xm.rsi.utils;

import com.xm.rsi.common.ErrorCode;
import com.xm.rsi.exception.BusinessException;
import net.sf.sevenzipjbinding.*;

import java.io.*;

public class ExtractCallback implements IArchiveExtractCallback {

    private final IInArchive inArchive;
    private final String ourDir;

    public ExtractCallback(IInArchive inArchive, String ourDir) {
        this.inArchive = inArchive;
        this.ourDir = ourDir;
    }

    @Override
    public void setCompleted(long arg0) {
    }

    @Override
    public void setTotal(long arg0) {
    }

    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        final String path = (String) inArchive.getProperty(index, PropID.PATH);
        final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER);
        final String[] oldPath = {""};
        return data -> {
            try {
                if (!isFolder) {
                    File file = new File(ourDir + File.separator + path);
                    boolean b = saveFile(file, data, path.equals(oldPath[0]));
                    if (!b) {
                        throw new BusinessException(ErrorCode.OPERATION_ERROR, "解压文件异常");
                    }
                    oldPath[0] = path;
                }
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "解压文件异常");
            }
            return data.length;
        };
    }

    @Override
    public void prepareOperation(ExtractAskMode arg0) {
    }

    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) {

    }

    //解决字节丢失  未验证
    public boolean saveFile(File file, byte[] msg, boolean append) {
        OutputStream fos;
        try {
            File parent = file.getParentFile();
            if ((!parent.exists()) && (!parent.mkdirs())) {
                return false;
            }
            fos = new FileOutputStream(file, append);//是否追加
            fos.write(msg);
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

//    public static boolean save2File(File file, byte[] msg) {
//        OutputStream fos = null;
//        try {
//            File parent = file.getParentFile();
//            if ((!parent.exists()) && (!parent.mkdirs())) {
//                return false;
//            }
//            fos = new FileOutputStream(file, true);
//            fos.write(msg);
//            fos.flush();
//            return true;
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            return false;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            try {
//                fos.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}


